<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table,td{
            border-collapse: collapse;
        }

        td{
            padding: 10px 20px;
        }
    </style>
</head>
<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        //创建虚拟 DOM 
        class App extends React.Component{
            state = {
                heros : [
                    {id:1, name: '孙膑', type: '辅助'},
                    {id:2, name: '小乔', type: '中单'},
                    {id:3, name: '扁鹊', type: '法师'}
                ]
            }

            //声明新增的方法
            add = () => {
                //获取名字
                let n = this.name.value;
                //获取类型
                let t = this.type.value;
                //创建英雄对象
                let hero = {
                    name: n,
                    type: t,
                    id: this.state.heros.length + 1
                };
                //创建新数组
                let newHeros = [
                    hero,
                    ...this.state.heros,
                ];
                //更新状态
                this.setState({
                    heros: newHeros
                })
               
            }

            render(){
                return <div>
                        名字: <input type="text" ref={el => this.name = el} />
                        类型: <input type="text" ref={el => this.type = el}/>
                        <button onClick={this.add}>新增</button>
                        <hr />
                        <table border="1">
                            <thead>
                                <tr><td>ID</td><td>名字</td><td>类型</td><td></td></tr>
                            </thead>    
                            <tbody>
                                {
                                    this.state.heros.map((item, index) => {
                                        return <tr key={index}><td>{item.id}</td><td>{item.name}</td><td>{item.type}</td><td><input type="text" /></td></tr>
                                    })
                                }
                            </tbody>
                        </table>
                    </div>
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));

    </script>


</body>
</html> 